package com.ys.zero.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ys.zero.bean.Concerned;
import com.ys.zero.bean.DZtb;
import com.ys.zero.bean.Dict;
import com.ys.zero.bean.Result;
import com.ys.zero.bean.SCtb;
import com.ys.zero.bean.Uarticle;
import com.ys.zero.bean.User;
import com.ys.zero.biz.BizException;
import com.ys.zero.biz.uarticleBiz;
import com.ys.zero.dao.UserDao;
import com.ys.zero.dao.articalDao;


@WebServlet("/artical/*")
public class uArticalServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
		private articalDao adao = new articalDao();
		private uarticleBiz abiz = new uarticleBiz();
		private UserDao udao =  new UserDao();

	protected void getData(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
			User user = (User) request.getSession().getAttribute("loginedUser");
			Integer id = user.getId();
			String content = request.getParameter("content");
			String type = request.getParameter("type");
			String log = request.getParameter("log");
			String utitle = request.getParameter("title");
//			System.out.println(id);
//			System.out.println(content);
//			System.out.println(type);
//			System.out.println(log);
//			System.out.println(utitle);
			try {
				abiz.judgeArticle(id, type, log, utitle, content);
				toJson(response, new Result(1, "文章发布成功"));
			} catch (BizException e) {
				toJson(response, new Result(0, e.getMessage()));
			}
		  
		   
	}
	protected void getType(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
	
		List<Dict> list =adao.getType();
		Gson g = new Gson();
		String json = g.toJson(list);
		response.getWriter().append(json);
	
	}
	protected void getLog(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
	
		List<Dict> list =adao.getLog();
		Gson g = new Gson();
		String json = g.toJson(list);
		response.getWriter().append(json);
	
	}
	protected void getAuthor(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		 String uaid =  request.getParameter("uaid");
		  Integer id =   Integer.parseInt(uaid);	
		  List<Uarticle>  list = adao.getUid(id);  
		  List<User>  list1 =  udao.getAll(list.get(0).getUid());

		  toJson(response, list1);
	}
	/**
	 * 粉丝等信息
	 * @param request
	 * @param response
	 * @throws Exception 
	 */
	protected void GetFence(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		 String uaid =  request.getParameter("uaid");
		  Integer id =   Integer.parseInt(uaid);	
		  List<Uarticle>  list = adao.getUid(id);  
		  Object  list1 =  udao.getFence(list.get(0).getUid());
		  List<Map<String,Object>>  list2 =  adao.getInfo(list.get(0).getUid()); 
		  List<Object> listAll = new ArrayList<Object>();
		  listAll.add(list1);
		  listAll.add(list2);
		  toJson(response, listAll);
	}

/**
 * 	分类专栏
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
	protected void GetFl(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String log = request.getParameter("FlLog");
		List<Uarticle> list =adao.showFLZL(log);
		toJson(response,list);
	}
	/**
	 * 得到分类文章的数量
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void GetFlCount(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String [] Logs  = request.getParameterValues("Logs");
		System.out.println("Logs: "+Logs);
		List<Object>  list  = new ArrayList<Object>();
		long f ;
		for (int i = 0; i < Logs.length; i++) {
			f = adao.getFlCount(Logs[i]);
			list.add(f);
		}
		toJson(response, list);
	
	}
	/**
	 * 点赞
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void dz(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String uaid  = request.getParameter("uaid");
		adao.addPraise(uaid);
	}
	/**
	 * 取消点赞
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void qxdz(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String uaid  = request.getParameter("uaid");
		adao.execPraise(uaid);
	}
	
	/**
	 * 收藏
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void sc(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String uaid  = request.getParameter("uaid");
		adao.addCollection(uaid);
	}
	/**
	 * 取消收藏
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void qxsc(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String uaid  = request.getParameter("uaid");
		adao.execCollection(uaid);
	}

	/**
	 * 点赞状态
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	protected void addDZ(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String uaid  = request.getParameter("uaid");
		User user = (User) request.getSession().getAttribute("loginedUser");
		Integer id = user.getId();
		Integer aid = Integer.parseInt(uaid);
		adao.addDZstate(aid, id);
	}	
	protected void dtDZ(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String uaid  = request.getParameter("uaid");
		User user = (User) request.getSession().getAttribute("loginedUser");
		Integer id = user.getId();
		Integer aid = Integer.parseInt(uaid);
		adao.dtDZstate(aid, id);
	}
	protected void qrDZ(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String uaid  = request.getParameter("uaid");
		User user = (User) request.getSession().getAttribute("loginedUser");
		Integer id = user.getId();
		Integer aid = Integer.parseInt(uaid);
		List<DZtb>  list   =    adao.qrDZstate(aid, id);
		toJson(response, list);
	}
	
	/**
	 * 收藏状态
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	protected void addSC(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String uaid  = request.getParameter("uaid");
		User user = (User) request.getSession().getAttribute("loginedUser");
		Integer id = user.getId();
		Integer aid = Integer.parseInt(uaid);
		adao.addSCstate(aid, id);
	}
	protected void dtSC(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String uaid  = request.getParameter("uaid");
		User user = (User) request.getSession().getAttribute("loginedUser");
		Integer id = user.getId();
		Integer aid = Integer.parseInt(uaid);
		adao.dtSCstate(aid, id);
	}
	protected void qrSC(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String uaid  = request.getParameter("uaid");
		User user = (User) request.getSession().getAttribute("loginedUser");
		Integer id = user.getId();
		Integer aid = Integer.parseInt(uaid);
		List<SCtb>  list   =    adao.qrSCstate(aid, id);
		toJson(response, list);
	}
	
	/**
	 * 获得visit数
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	protected void insertVisit(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String uaid  = request.getParameter("uaid");
		adao.insertVisit(uaid);
	}
	
}
